﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LEARNREN.COM.Entity.Enum;
using LEARNREN.COM.BLL;
using LEARNREN.COM.Entity;
using LEARNREN.COM.UI;

namespace LEARNREN.COM.WebSite.UserDefinedControls
{
    public partial class SelectCatalogueDropDownList : System.Web.UI.UserControl
    {
        #region 属性

        /// <summary>
        /// 可以点击的节点类型
        /// </summary>
        public CatalogueTypeEnum CanClickNodeType { get; set; }

        /// <summary>
        /// 处理逻辑用的对象
        /// </summary>
        public CatalogueLogic CatalogueLogicProvider { get; set; }

        /// <summary>
        /// 文本框样式
        /// </summary>
        public string CssClass
        {
            get
            {
                return txtSelectedCatalogueTitle.CssClass;
            }
            set
            {
                txtSelectedCatalogueTitle.CssClass = value;
            }
        }

        /// <summary>
        /// 获取或设置选中的栏目的Id
        /// </summary>
        public long SelectedCatalogueId
        {
            get
            {
                return long.Parse(this.txtSelectedCatalogueId.Value);
            }
            set
            {
                SetSelectItem(value);
            }
        }

        /// <summary>
        /// 选中的栏目路径，当没有选中时返回空字符串
        /// </summary>
        public string SelectedCataloguePath
        {
            get
            {
                return this.txtSelectedCataloguePath.Value;
            }
        }

        /// <summary>
        /// 是否启用验证
        /// </summary>
        public bool EnableValidation
        {
            get
            {
                return CatalogueCustomValidator.Enabled;
            }
            set
            {
                CatalogueCustomValidator.Enabled = value;
                CatalogueCustomValidator.ClientValidationFunction = ValidationMethodName;
            }
        }

        /// <summary>
        /// 验证组
        /// </summary>
        public string ValidationGroup
        {
            get
            {
                return CatalogueCustomValidator.ValidationGroup;
            }
            set
            {
                CatalogueCustomValidator.ValidationGroup = value;
            }
        }

        /// <summary>
        /// 客户端验证方法名称
        /// </summary>
        protected string ValidationMethodName
        {
            get
            {
                return this.ID + "ValidationFunction";
            }
        }

        #endregion 属性

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitTree();                
            }
            RegistInitJs();
        }

        private void InitTree()
        {
            this.CatalogueTree1.CanClickNodeType = this.CanClickNodeType;
            this.CatalogueTree1.CatalogueLogicProvider = this.CatalogueLogicProvider;
            this.CatalogueTree1.CanClickRootNode = true;
        }

        protected void CatalogueTree1_SelectedNodeChanged(object sender, DelegateAndArges.CatalogueTreeSelectedNodeChangedEventArgs e)
        {
            long catalogueId = e.SelectedCatalogueId;
            SetSelectItem(catalogueId);
            ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "$('#" + this.ID + "Tree').hide();", true);
        }

        /// <summary>
        /// 使用指定值设置选中项
        /// </summary>
        /// <param name="catalogueId"></param>
        private void SetSelectItem(long catalogueId)
        {
            Catalogue catalogue = CatalogueLogicProvider.GetById(catalogueId);
            if (catalogue != null)
            {
                this.txtSelectedCatalogueId.Value = catalogueId.ToString();
                this.txtSelectedCataloguePath.Value = catalogue.Path;
                this.txtSelectedCatalogueTitle.Text = catalogue.Title;
            }
            else
            {
                this.txtSelectedCatalogueId.Value = "0";
                this.txtSelectedCataloguePath.Value = "";
                this.txtSelectedCatalogueTitle.Text = "--请选择--";
            }
        }

        #region 公有方法

        /// <summary>
        /// 重新加载
        /// </summary>
        public void Reload()
        {
            InitTree();
            CatalogueTree1.Reload();
        }

        public void RegistInitJs()
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "init" + this.ID + "()", true);
        }

        #endregion  共有方法
    }
}